<template>
	<view class="init-page">

	</view>
</template>

<script>
	import {
		version,
		domain,
		spareDomain
	} from "@/config.js"
	export default {
		data() {
			return {
				isLogin: false,
			}
		},
		created() {
			this.getAvailableDomain(domain, this.nextGoPage)
		},
		methods: {
			nextGoPage() {
				let currentVersion = uni.getStorageSync('first_launch') || '0.0'
				if (currentVersion === version) { //需要欢迎页 可以把true加上 true || currentVersion === version
					this.fetchLoginInfo((isLogin) => {
						//#ifdef APP-PLUS
						setTimeout(() => {
							plus.navigator.closeSplashscreen()
						}, 300)
						//#endif
						if (isLogin) {
							uni.reLaunch({
								url: '/pages/index/index'
							})
						} else {
							uni.reLaunch({
								url: '/pages/login/index'
							})
						}

					})
				} else {
					uni.setStorageSync('first_launch', version)
					//#ifdef APP-PLUS
					plus.navigator.closeSplashscreen()
					//#endif
					uni.reLaunch({
						url: '/pages/guide/guide'
					});
				}
			},
			// 先请求主域名, 有效就使用主域名
			//失效就请求备用域名, 有效就是用备用域名
			//备用域名失效, 只能使用主域名了
			getAvailableDomain(urlDomain, callback) {
				console.log(`正在检测${urlDomain}是否可用`)
				let timer = null
				let requestTask = uni.request({
					url: `${urlDomain}api/config`,
					timeout: 3000,
					success: () => {
						uni.setStorageSync('available_domain', urlDomain)
						console.log(`${urlDomain}可用`)
						callback()
					},
					complete: () => {
						clearTimeout(timer)
					},
					fail: () => {
						console.log(`${urlDomain}不可用`)
						if (urlDomain === domain) {
							this.getAvailableDomain(spareDomain, callback)
						} else {
							uni.setStorageSync('available_domain', domain)
							callback()
						}
					}
				})
				timer = setTimeout(() => {
					requestTask.abort()
					console.log('--手动取消--')
				}, 3000)
			},
			fetchLoginInfo(fn) {
				this.$post('api/ucenter/info').then(res => {
					this.isLogin = true
					this.$setStore('ucenterInfo', res.data ,true)
					fn(true)
				}).catch((res) => {
					fn(false)
				});
			},
		}
	};
</script>

<style></style>
